/*
 * Copyright (C) 2008  Wang Hoi <fearee@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
*/
#ifndef MEMCACHE_H
#define MEMCACHE_H
#include <QtCore>
#include <memcached.h>

#define MAX_VALUE_SIZE 1024
class MemCache: public QObject
{
    Q_OBJECT
public:
    MemCache(int threads = -1);
    ~MemCache();
    void setServers(const QList<QByteArray> &servers);
    void get(const QString &key);
    void set(const QString &key, const QByteArray &value);
    void del(const QString &key);
Q_SIGNALS:
    void valueGot(const QString &key, const QByteArray &value);
    void valueSet(const QString &key, const QByteArray &value);
    void valueDel(const QString &key);
    void errorGot(const QString &err);
private:
    class Private;
    Private *d;
};
namespace MemCachePrivate
{
class GetJob: public QObject, public QRunnable
{
    Q_OBJECT
public:
    GetJob(memcached_st *mc, const QString &key);
    void run();
Q_SIGNALS:
    void done(const QString &key, const QByteArray &value);
    void errorGot(const QString &err);
private:
    QString key;
    memcached_st *mc;
};

class SetJob: public QObject, public QRunnable
{
    Q_OBJECT
public:
    SetJob(memcached_st *mc, const QString &key, const QByteArray &value);
    void run();
Q_SIGNALS:
    void done(const QString &key, const QByteArray &);
    void errorGot(const QString &err);
private:
    QString key;
    QByteArray value;
    memcached_st *mc;
};

class DelJob: public QObject, public QRunnable
{
    Q_OBJECT
public:
    DelJob(memcached_st *mc, const QString &key);
    void run();
Q_SIGNALS:
    void done(const QString &key);
    void errorGot(const QString &err);
private:
    QString key;
    memcached_st *mc;
};
} // namespace MemCachePrivate
#endif // MEMCACHE_H
